

/**
 * 画图基类
 */

import MouseEvent from "../../event/types/MouseEvent"

export default class Draw {

  constructor() {
    this._viewer = undefined
    this._delegate = undefined
    this._floatingAnchor = undefined
    this._clampToGround = true
    this._layer = undefined
    this._plotEvent = undefined
    this._tooltip = undefined
    this._mouseEvent = undefined
    this._message = undefined
    this._options = {}
    this._positions = []
  }

  _mountEntity() { }

  _onClick(e) { }

  _onMouseMove(e) {
    e = this.getMouseInfo(e.endPosition)
    this._tooltip.showAt(e.windowPosition , this._message)
    if (this._floatingAnchor) {
      
      let position = this._clampToGround ? e.surfacePosition : e.position
      if (!position) {
        return false
      }
      this._floatingAnchor.position.setValue(position)
      this._positions.pop()
      this._positions.push(position)
    }
  }

  _onRightClick(e) { }

  /**
   * 绑定鼠标事件
   */
  bindEvent() {
    this._mouseEvent.on(Cesium.ScreenSpaceEventType.LEFT_CLICK, this._onClick, this)
    this._mouseEvent.on(Cesium.ScreenSpaceEventType.MOUSE_MOVE, this._onMouseMove, this)
    this._mouseEvent.on(Cesium.ScreenSpaceEventType.RIGHT_CLICK, this._onRightClick, this)
  }

  /**
   * 移出鼠标事件
   */

  unbindEvent() {
    this._tooltip.enable = false
    this._mouseEvent.off(Cesium.ScreenSpaceEventType.LEFT_CLICK, this._onClick, this)
    this._mouseEvent.off(Cesium.ScreenSpaceEventType.MOUSE_MOVE, this._onMouseMove, this)
    this._mouseEvent.off(Cesium.ScreenSpaceEventType.RIGHT_CLICK, this._onRightClick, this)
    this._mouseEvent.destroy()
  }

  /**
   * 开始画图
   * @param {*} plot 
   * @param {*} clampToGround 
   */

  start(plot, clampToGround) {
    this._viewer = plot.viewer
    this._tooltip = plot.tooltips
    this._tooltip.enable = true
    this._layer = plot.overlayLayer
    this._plotEvent = plot.plotEvent
    this._options = plot.options
    this._clampToGround = clampToGround
    this._mouseEvent = new MouseEvent(this._viewer)
    this._mountEntity()
    this.bindEvent()
  }

  /**
   * 获取鼠标对象信息
   * @param {*} position 
   * @returns 
   */

  getMouseInfo(position) {
    let scene = this._viewer.scene
    let target = scene.pick(position)
    let cartesian = undefined
    let surfaceCartesian = undefined
    let wgs84Position = undefined
    let wgs84SurfacePosition = undefined
    if (scene.pickPositionSupported) {
      cartesian = scene.pickPosition(position)
    }
    if (cartesian) {
      let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian)
      if (c) {
        wgs84Position = {
          lng: Cesium.Math.toDegrees(c.longitude),
          lat: Cesium.Math.toDegrees(c.latitude),
          alt: c.height
        }
      }
    }
    if (
      scene.mode === Cesium.SceneMode.SCENE3D &&
      !(this._viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)
    ) {
      let ray = scene.camera.getPickRay(position)
      surfaceCartesian = scene.globe.pick(ray, scene)
    } else {
      surfaceCartesian = scene.camera.pickEllipsoid(
        position,
        Cesium.Ellipsoid.WGS84
      )
    }
    if (surfaceCartesian) {
      let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(surfaceCartesian)
      if (c) {
        wgs84SurfacePosition = {
          lng: Cesium.Math.toDegrees(c.longitude),
          lat: Cesium.Math.toDegrees(c.latitude),
          alt: c.height
        }
      }
    }

    return {
      target: target,
      windowPosition: position,
      position: cartesian,
      wgs84Position: wgs84Position,
      surfacePosition: surfaceCartesian,
      wgs84SurfacePosition: wgs84SurfacePosition
    }
  }

  /**
   *
   * @param position
   * @param isCenter
   * @returns {*}
   */
  createAnchor(position, isCenter = false) {
    return this._layer.add({
      position: position,
      billboard: {
        image: isCenter ? this._options.icon_center : this._options.icon_anchor,
        width: this._options.icon_size[0],
        height: this._options.icon_size[1],
        eyeOffset: new Cesium.Cartesian3(0, 0, -500),
        heightReference:
          this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
            this._clampToGround
            ? Cesium.HeightReference.CLAMP_TO_GROUND
            : Cesium.HeightReference.NONE
      }
    })
  }
}